<?php

namespace Fin;

use C\L\AdmController;

class InvestController extends AdmController
{



    protected function init()
    {
        $this->service = $this->s_invest;

        $this->keyworkSearchKeys = [
        'uid',
        ];
        $this->hideKeys = [
        'is_delete'
        ];

        $this->timeToDateKeys = [
        'uptime', 'addtime'
        ];

        $this->pubSearchKeys = [
        'status', 'channel', 'reg_channel'
        ];

        $this->regChannelSearchKeys = [
      // 'reg_channel'
        ];
    }

    public function verifyAction()
    {
        $status = $this->getValue('status', true, 'string');
        $id     = $this->getValue('id', true, 'int');

        $this->s_invest->verify($id, $status)
        ? $this->success()
        : $this->error();
    }

    protected function afterSearch(&$data)
    {
        $data['config'] = $this->s_invest->getStatusConfig();
        $data['config']['reg_channel'] = $this->s_user->getStatusConfig()['channel'];
        $reg_channel = $data['config']['reg_channel'];
        foreach ($data['list'] as &$item) {
            $user = $this->s_user->search($item['uid']);
            $item['name'] = $user['name'] ?? '';
            $item['mobile'] = $user['mobile'] ?? '';
            $item['note'] = $user['note'] ?? '';
          // $item['reg_channel'] = $user['channel'];
            $item['reg_channel_name'] = isset($item['reg_channel']) ? $reg_channel[$item['reg_channel']] : '';
        }

        $this->params['data']['status'] = 'Y';
        $data['sum_ok_money'] = $this->s_invest->getSum('money', $this->params['data'], $this->params['data_type']);
        $this->params['data']['status'] = 'N';
        $data['sum_no_money'] = $this->s_invest->getSum('money', $this->params['data'], $this->params['data_type']);
        $this->params['data']['status'] = ['S', 'D'];
        $this->params['data_type']['status'] = 'in';
        $data['sum_on_money'] = $this->s_invest->getSum('money', $this->params['data'], $this->params['data_type']);
        return true;
    }

    public function cancelsAction()
    {
        $ids = $this->getValue('ids', true);
        if ($this->s_invest->updates(
            ['status' => 'C', 'front_status' => 'C'],
            ['id' => $ids, 'status' => ['S', 'D']],
            ['id' => 'in', 'status' => 'in']
        )) {
            $this->success();
        }

        $this->error();
    }
}
